package com.foreverwin.jzymes.production.clamp.controller;

import com.foreverwin.jzymes.common.domain.CarrierScanLog;
import com.foreverwin.jzymes.common.util.CarrierUtil;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.util.ObjectMapperUtil;
import com.foreverwin.jzymes.production.clamp.service.IClampAdviceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/production/clamp")
public class ClampController {

    private Logger logger = LoggerFactory.getLogger( ClampController.class );

    @Autowired
    IClampAdviceService clampAdviceService;

    @RequestMapping("/carrierScan")
    public AjaxResult clampCarrier( @RequestBody String carrierScanLogJson ){
        logger.info( "接收到装夹信息: " + carrierScanLogJson );
        try{
            CarrierScanLog carrierScanLog = ObjectMapperUtil.readValueForUnderscore( carrierScanLogJson, CarrierScanLog.class );
            carrierScanLog.setCarrier( CarrierUtil.buildCarrier( carrierScanLog.getSite(), carrierScanLog.getCarrier() ) );
            clampAdviceService.clampCarrier( carrierScanLog );
            logger.info( "装夹信息处理完成: " + carrierScanLogJson );
            return AjaxResult.success();
        }catch ( Exception e ){
            logger.error( "装夹信息处理失败: " + e.getMessage() + "，装夹信息: " + carrierScanLogJson );
            return AjaxResult.error( e.getMessage() );
        }
    }
}
